package com.javase.多线程.线程间共享数据;

/**
 * @Auther: liyanhao
 * @Description: 线程间共享数据2
 * @Date: 2022/5/18 01:43
 * @Version: v1.0
 */
public class ThreadData2 {
    private int j = 0;
    public synchronized void add(){
        j++;
        System.out.println("线程"+Thread.currentThread().getName()+"j 为："+j);
    }
    public synchronized void dec(){
        j--;
        System.out.println("线程"+Thread.currentThread().getName()+"j 为："+j);
    }
    public int getData(){
        return j;
    }
}
class TestThread{
    public static void main(String[] args) {
        ThreadData2 threadData2 = new ThreadData2();
        for (int i = 0; i < 2; i++) {
            new Thread(()->{
                threadData2.add();
            }).start();
            new Thread(()->{
                threadData2.dec();
            }).start();
        }

    }
}
